﻿using H2.NET.Business;
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using H2.NET.Common;
using H2.NET.Data.Common;
using H2.NET.Web.Workflow.Helper;
using System.Linq;

namespace H2.NET.Web.Controls
{
    public partial class DwzAssessment : TaskControl
    {
        public bool Enable = true;
        public string Category = null;
        public List<t_AssessmentOption> Option = new List<t_AssessmentOption>();
        public string Duty = "";
        public string remark
        {
            get
            {
                return ViewState["remark"] == null ? "状态备注" : ViewState["remark"].ToString();

            }
            set { ViewState["remark"] = value; }
        }
        public bool IsReturned
        {
            get
            {
                return ViewState["isReturned"] != null && (bool)ViewState["isReturned"];

            }
            set { ViewState["isReturned"] = value; }
        }

        public bool NotNeedShow
        {
            get
            {
                return ViewState["NotNeedShow"] != null && (bool) ViewState["NotNeedShow"];
            }
            set { ViewState["NotNeedShow"] = value; }
        }

        public bool Cashier
        {
            get
            {
                return ViewState["Cashier"] != null && (bool)ViewState["Cashier"];

            }
            set { ViewState["Cashier"] = value; }
        }


        #region 事件 Load (页面)
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
            LoadOptions();

            //重转“选择项”
            var selected = 0;
            RadOpition.Items.Clear();

            for (var i = 0; i < Option.Count; i++)
            {
                if (Option[i].RadName != "驳回")
                {
                    if (Option[i].RadName == "总部不发机")
                    {
                        cancel.Visible = true;
                    }

                    if (Option[i].RadName == "转至三方转移")
                    {
                        transfer.Visible = true;
                    }
                }
                else
                {
                    rebutRemark.Visible = true;
                }
                RadOpition.Items.Add(new ListItem(Option[i].RadName, Option[i].RadValue));

                //取得默认值
                if (Option[i].IsDefault)
                {
                    selected = i;
                }
            }

            //初始化“选择项”选择、备注框对应值
            if (Option.Count > 0)
            {
                RadOpition.Items[selected].Selected = true;
            }

            //设置是否只读
            var objects = TaskAPI["Actions"] as object[];
            if (objects != null)
            {
                var key = objects[0].ToString();
                StepLabel.Text = GetCache(TaskAPI.Step);
                if (key.Contains("Contract") && TaskAPI.Step == "Manager Approve")
                {
                    StepLabel.Text = @"全国合同信息确认";
                }
                if (TaskAPI.Step == "Finance Change" && TaskAPI.Process == "Store Deposit Change")
                {
                    StepLabel.Text = @"财务审核";
                }
                if (TaskAPI.Step == "Finance Confirm" && TaskAPI.Process == "Store Deposit Change")
                {
                    StepLabel.Text = @"财务确认";
                }
            }
            RadOpition.Enabled = Enable;
            Remark.Enabled = Enable;
        }
        #endregion

        #region 加载选项
        private void LoadOptions()
        {
            var options = (t_AssessmentOptions._Status == 1 & t_AssessmentOptions._Process == TaskAPI.Process
                & t_AssessmentOptions._Step == TaskAPI.Step).List<t_AssessmentOptions>();
            var dutys = (t_AssessmentDuty._Process == TaskAPI.Process & t_AssessmentDuty._Step == TaskAPI.Step
               & t_AssessmentDuty._Category == Category).List<t_AssessmentDuty>();
            if (options.Count > 0)
            {
                Option.Clear();
            }
            if (NotNeedShow && TaskAPI.Process =="New Store Deposit Receive")
            {
                options = options.Where(t => t.Step != "Submit Document").ToList();
            }
            foreach (var opt in options)
            {
                if (IsReturned || opt.isReturned == "0")
                { }
                else
                {
                    Option.Add(new t_AssessmentOption(opt.OptionName, opt.OptionValue, false));
                }
            }

            if (NotNeedShow)
            {
                Option = Option.Where(t => t.RadName != "总部不发机").ToList();
            }

            if (!Cashier)
            {
                Option = Option.Where(t => t.RadName != "出纳已退款").ToList();
            }

            if (dutys != null && dutys.Count > 0 && !string.IsNullOrEmpty(dutys[0].Duty))
            {
                L_Duty.Text = dutys[0].Duty;
                td_Duty.Style.Remove("display");
            }
        }
        #endregion

        #region 方法 提取 Assessment
        public t_Assessment Assessment
        {
            get
            {
                var ass = new t_Assessment();
                ass.Process = TaskAPI.Process;
                ass.Incident = TaskAPI.Incident;
                ass.Step = TaskAPI.Step;
                ass.UserName = TaskAPI.UserName;
                ass.UserFullName = TaskAPI.UserFullName;
                ass.AssignedToUserName = UserAPI.UserName;
                ass.AssignedToUserFullName = UserAPI.UserFullName;
                ass.Department = UserAPI.Department;
                ass.StartTime = TaskAPI.StartTime;
                ass.Time = DateTime.Now;
                ass.Opinion = RadOpition.SelectedValue;
                ass.OpinionText = RadOpition.SelectedItem == null ? "" : RadOpition.SelectedItem.Text;
                ass.Opinion2 = "";
                ass.OpinionText2 = "";
                ass.Status = 1;
                foreach (ListItem li in RadOpition.Items)
                {
                    ass.Opinion2 += li.Value + "~";
                    ass.OpinionText2 += li.Text + "~";
                }
                ass.Remark = Remark.Text;

                var autoBusinessKey = TaskAPI.GetVariableBySingleValue("AutoBusinessKey");
                ass.BusinessProcess = autoBusinessKey;
                var parentProcess = TaskAPI.GetVariableBySingleValue("ParentProcess");
                var parentIncident = TaskAPI.GetVariableBySingleValue("ParentIncident");
                //如果没有父流程，说明他是一个新发起的流程
                if (parentProcess == "" && parentIncident == "")
                {
                    var assessment = (t_Assessment.process == ass.Process & t_Assessment.incident == ass.Incident)
                        .ListFirst<t_Assessment>();
                    //如果是第一步就新建，其他的就找有的
                    if (assessment != null && assessment.BusinessKey != "")
                    {
                        ass.BusinessKey = assessment.BusinessKey;
                    }
                    else
                    {
                        var key = "";
                        var num = "0001";
                        switch (ass.BusinessProcess)
                        {
                            case Constant.PROCESS_CHANGEDEALER:
                                key = "DC";
                                break;
                            case Constant.PROCESS_CHANGELEVEL:
                                key = "SD";
                                break;
                            case Constant.PROCESS_CHANGEOTHER:
                                key = "MC";
                                break;
                            case Constant.PROCESS_CHANGESTATUS:
                                key = "DD";
                                break;
                            case Constant.PROCESS_STORE:
                                key = "NS";
                                break;
                            case Constant.PROCESS_TRANSFER:
                                key = "DT";
                                break;
                            case Constant.PROCESS_BATCHDEPOSIT:
                                key = "BD";
                                break;
                            case Constant.PROCESS_MDCITSS:
                                key = "BH";
                                break;
                        }
                        var timeKey = key + DateTime.Now.Year.ToString() + (DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString(): DateTime.Now.Month.ToString());
                        var ass1 = (t_Assessment.businessKey.Like(timeKey + "%")).ListFirst<t_Assessment>("BusinessKey desc");

                        if (ass1 != null)
                        {
                            var businessKey = ass1.BusinessKey;
                            var numBak = Utils.StrToInt(businessKey.Substring(businessKey.Length - 4), 0) + 1;
                            var numKey = "";
                            numKey = numBak.ToString();
                            for (var s = numBak.ToString().Length; s < 4; s++)
                            {
                                numKey = "0" + numKey;
                            }
                            timeKey += numKey;
                        }
                        else
                        {
                            timeKey += num;
                        }
                        ass.BusinessKey = timeKey;



                    }
                }
                else
                {
                     var assessment = (t_Assessment.process == parentProcess & t_Assessment.incident == parentIncident)
                        .ListFirst<t_Assessment>();
                    ass.BusinessKey = assessment.BusinessKey;
                }


                return ass;
            }
        }
        #endregion

        #region 获取缓存数据
        public string GetCache(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return string.Empty;
            }

            return Cache[key.Trim()] == null ? key : Cache[key.Trim()].ToString();
        }
        #endregion
    }
}